<?php
require "../includes/db.php"; 
$mysqli = new MossCMS();
if ($_SESSION[$prefix.'_username'] == ""){  header("Location: ../login.php"); exit;}
if ($_SESSION[$prefix."_security"] < 1){header("location:../login.php?action=5"); exit;}



$site_thumb_size 		= 200;
$default_image 			= 250;

if ($_SERVER['REQUEST_METHOD'] == "POST") { 

$date	= date("Y-m-d H:i:s");

   $newsdate = $_POST['newsdate'];
   $title    = $_POST['title'];
   $content  = $_POST['editor1'];
   $picture  = $_POST['picture'];
   $picture_up = $_POST['picture_up'];



include "includes/cleanfile.php";
include "includes/rmkdir.php";      

$thumbnail = $default_image;
$new_name = cleanfile($title);
$stamp = date("His");
$thumb_name = $new_name.$stamp."_tn.jpg";
$m_name =  $new_name.$stamp."_m.jpg";
$s_name =  $new_name.$stamp."_s.jpg";

if($picture != ''){
$current_upload_file_loc = $picture;
$current_upload_file_name = $title.$stamp."_tn.jpg";
}

$upload_folder = "news/";
$temp_loc = $global_upload_dir.$upload_folder;
$http_loc = "uploads/".$upload_folder;
mkdir($temp_loc);
if(($_FILES['picture_up']['name']) != ""){
$new_name = cleanfile(basename($_FILES['picture_up']['name']));
	if(move_uploaded_file($_FILES['picture_up']['tmp_name'], $temp_loc.$new_name)){
		$current_upload_file_loc = $temp_loc.$new_name;
	}
}

$file_loc = $current_upload_file_loc;


if($file_loc != ''){

//CREATE THUMBNAILS
require("includes/resize-class.php");
	//THUMBNAIL
	$resizeObj = new resize($file_loc);
	$resizeObj -> resizeImage(250, 100, 'landscape');
	$resizeObj -> saveImage($temp_loc.$thumb_name, 100);
	//MEDIUM
	$resizeObj = new resize($file_loc);
	$resizeObj -> resizeImage(500, 100, 'landscape');
	$resizeObj -> saveImage($temp_loc.$m_name, 100);
	//SQUARE
	$resizeObj = new resize($file_loc);
	$resizeObj -> resizeImage(75, 75, 'crop');
	$resizeObj -> saveImage($temp_loc.$s_name, 100);

$thumbnail = $http_loc.$thumb_name;
$medium = $http_loc.$m_name;
$square = $http_loc.$s_name;

}else{
	$image = $default_image;
	$thumbnail = $default_image;
}


if($thumbnail == ''){$thumbnail = $default_image;}
	$type = 1; $image=$medium; $thumb=$thumbnail; $loc = '';
		
		$mysqli->content_add($type,$title,$image,$thumb,$newsdate,$content,$loc);
		$last_id = $mysqli->insert_id;
		
	$results = $mysqli->groups_list();
   	for($x=0; $x < sizeof($results); $x++) {
      $current_group = $results[$x]['group_id'];
		if($_POST[$current_group] != ''){
			
			$mysqli->content_group_add($last_id, $current_group);
	
		}	

	}	
		
		
		$mysqli->admin_action($z_user, "Added news $title for $newsdate");

header("Location:news_list.php");

}//END POST 
//*************************



$date = date("Y-m-d");

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="../includes/admin.css"/>
<title><?php echo $global_title;?></title>

<?php echo $jquery_global; ?>
<?php echo $jquery_ui_global; ?>
<?php echo $ckeditor_global; ?>
<script> 
	$(function() {
		$( "#newsdate" ).datepicker({ dateFormat: 'yy-mm-dd', changeMonth: true, changeYear: true });
	});
	</script> 
  


<script language="javascript" type="text/javascript">
    var submitcount = 0;
    function verify()
    {
      // Check for empty fields
      
      if(this.document.form1.newsdate.value == "")
      {
        alert("Please Enter a Date.");
        this.document.form1.newsdate.focus();
        return false;
      }
      
	   if(this.document.form1.title.value == "")
      {
        alert("Please Enter a Title.");
        this.document.form1.title.focus();
        return false;
      }
	  
      if(submitcount == 0)
      {
        submitcount++;
        return true;
      }
      else
      {
        alert("This form has already been submitted.  Thanks!");
        return false;
      }
    }
</SCRIPT> 
</head>
<body>

<div id="wrap">
	<div id="page-header">
		<?php include('includes/header.php');?>

	</div>
	
	<div id="page-body">
		<div id="tabs">
			<?php $tab="main"; include('includes/header_tabs.php');?>
		</div>

		<div id="acp">
		<div class="panel">
			<span class="corners-top"><span></span></span>
				<div id="content">
				<div id="menu">
						
					<?php include('includes/menu_top.php');?>	
					<?php include('includes/menu_main.php');?>
					</div>
	
					<div id="main">
<a name="maincontent"></a>


	<h1>Add News</h1>
	
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1" onSubmit="return verify();">
	  
        <fieldset><legend>Article Information</legend>
		 
		 <dl>
              <dt>Title:</dt>
              <dd><input name="title" type="text" id="title" size="60" autofocus required /></dd>
            </dl>
 
		  
		<dl>
              <dt>Date:</dt>
              <dd><input type="date" name="newsdate" id="newsdate" required  value="<?php echo $date; ?>" /></dd>
          </dl>
                                  
            <p>Main Story:</p>
            <textarea name="editor1" id="editor1" class="nicedit_textarea" style="height: 300px;"></textarea>
           </fieldset>
           
       <fieldset><legend>Access</legend>
         <p>Assign which groups will have access to this content</p>  
         <table border=0 width="100%">
			<tr>
         <?php $y=0;
         $results = $mysqli->groups_list();
      			for($x=0; $x < sizeof($results); $x++) {
      			$y++;
      			if($y == 5){echo "</tr><tr>"; $y= 0;}
         	?>
         	<td><input type="checkbox" id="<?php echo $results[$x]['group_id'];?>" name="<?php echo $results[$x]['group_id'];?>" value="<?php echo $results[$x]['group_id'];?>" > <?php echo $results[$x]['group_name'];?></td>
         	<?php } ?>
           </tr></table>	
        </fieldset>   
           
           <fieldset><legend>Picture</legend>   
            <dl>
              <dt>Upload Picture:</dt>
              <dd><input type="file" name="picture_up" id="picture_up" ></dd>
            </dl>
            <dl>
              <dt>Or Enter Picture URL:</strong></dt>
              <dd><input name="picture" type="text" id="picture" size="60" /></dd>
            </dl>
           </fieldset> 
                                   
<p class="submit-buttons"><input type="submit" class="button1" name="Add News" id="submit" value="Add News" /></p>

    <br>   

    </form>

		</div><!-- main -->
	</div><!-- content -->
<span class="corners-bottom"><span></span></span>
</div><!-- panel -->
</div><!-- acp -->

	
	<div id="page-footer">
	<?php include('includes/footer.php');?>
				</div>
</div><!-- page-body -->
</div><!-- wrap -->

   <script type="text/javascript">
   
	CKEDITOR.replace( 'editor1', {toolbar : 'MyToolbar'} );
	
	CKEDITOR.config.width = '98%';
	CKEDITOR.config.height = '300';
	CKEDITOR.config.resize_maxWidth = '98%';
	CKEDITOR.config.toolbarStartupExpanded = 'true';
	
</script>

</body> 
</html>